package com.example.wly.application_3_homework;

import android.Manifest;
import android.content.Intent;
import android.net.Uri;
import android.support.annotation.NonNull;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

import java.util.HashMap;

import pub.devrel.easypermissions.EasyPermissions;

public class MainActivity extends AppCompatActivity {
    HashMap<String,String> phoneNumber;
    EditText number;
    EditText etKey;
    EditText etkeyNum;
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
        EasyPermissions.onRequestPermissionsResult(requestCode,permissions,grantResults,this);
    }
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        requestMyAppPermission();
        initPhoneNumber();
        number=(EditText)findViewById(R.id.tv_number);
        Button call=(Button)findViewById(R.id.btn_call);
        Button key=(Button)findViewById(R.id.btn_key);
        Button KeyNum=(Button)findViewById(R.id.btn_put);
        etKey=(EditText)findViewById(R.id.et_key);
        etkeyNum=(EditText)findViewById(R.id.et_keyNumber);
        KeyNum.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String key=etKey.getText().toString().trim();
                String keyNUmber=etkeyNum.getText().toString().trim();
                phoneNumber.put(key,keyNUmber);
            }
        });
        MyLinster linsten=new MyLinster();
        call.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String cutNumber=number.getText().toString().trim();
                Intent intent=new Intent();
                intent.setAction(Intent.ACTION_CALL);
                intent.setData(Uri.parse("tel:"+cutNumber));
                startActivity(intent);
            }
        });
        key.setOnClickListener(linsten);
    }

    private void initPhoneNumber() {
       phoneNumber =new HashMap<>();
        phoneNumber.put("10","10086");
        phoneNumber.put("11","10010");
        phoneNumber.put("12","120");
    }

    private void requestMyAppPermission(){
        String[]perms={Manifest.permission.CALL_PHONE};

        if(!EasyPermissions.hasPermissions(this,perms)){
            EasyPermissions.requestPermissions(this,"call phone",1,perms);
        }
    }

    class MyLinster implements View.OnClickListener{

        @Override
        public void onClick(View v) {
            String cutNumber=number.getText().toString().trim();

            Intent intent=new Intent();
            intent.setAction(Intent.ACTION_CALL);
            String keynuber=phoneNumber.get(cutNumber);
            if(keynuber.equals("")){
                intent.setData(Uri.parse("tel:"+cutNumber));
            }else {
                intent.setData(Uri.parse("tel:"+keynuber));
            }
            startActivity(intent);
        }
    }
}
